export default{
  namespaced:true,
  state:()=>({
    // 六个必须的属性
    // goods_id,goods_name,goods_price,goods_count,goods_small_logo,goods_state
    cart:JSON.parse(uni.getStorageSync('cart')||'[]')
  }),
  mutations:{
    addToCart(state,goods){
     const result = state.cart.find(x=>x.goods_id===goods.goods_id)
     if(!result){
       state.cart.push(goods)
     }else{
       result.goods_count++
     }
     this.commit('m_cart/saveToStorage')
    },
    saveToStorage(state){
      uni.setStorageSync('cart',JSON.stringify(state.cart))
    },
    updateGoodsState(state,goods){
     const result = state.cart.find(x=>x.goods_id===goods.goods_id)
     if(result){
       result.goods_state = goods.goods_state
     }
      this.commit('m_cart/saveToStorage')
    },
    removeGoodsItem(state,goods){
      state.cart= state.cart.filter(x=>x.goods_id!==goods.goods_id)
       this.commit('m_cart/saveToStorage')
    }
  },
  getters:{
    total(state){
      let c=0
      state.cart.forEach(x=>c+=x.goods_count)
      return c
    }
  }
}